package cn.iocoder.boot.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.*;
import org.apache.ibatis.type.JdbcType;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 组件价格
 *
 * @author 芋道源码
 */
@Data
@TableName("kb_component_price")
@KeySequence("component_price_seq")
@ToString(callSuper = true)
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ComponentPrice {
    /**
     * 创建时间
     */
//    @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createTime;
    /**
     * 最后更新时间
     */
//    @TableField(fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updateTime;
    /**
     * 创建者，目前使用 SysUser 的 id 编号
     * <p>
     * 使用 String 类型的原因是，未来可能会存在非数值的情况，留好拓展性。
     */
    @TableField(fill = FieldFill.INSERT, jdbcType = JdbcType.VARCHAR)
    private String creator;
    /**
     * 更新者，目前使用 SysUser 的 id 编号
     * <p>
     * 使用 String 类型的原因是，未来可能会存在非数值的情况，留好拓展性。
     */
    @TableField(fill = FieldFill.INSERT_UPDATE, jdbcType = JdbcType.VARCHAR)
    private String updater;
    /**
     * 编号
     */
    @TableId(type = IdType.AUTO)
    private Long id;
    /**
     * 库存编号
     */
    private Long stockId;
    /**
     * 折扣价JSON
     */
    private String json;
    /**
     * 库存数量
     */
    private Long stock;
    /**
     * 包装
     */
    private String packaging;

    /**
     * 封装
     */
    private String encapsulation;

    /**
     * 型号
     */
    private String model;
    /**
     * 货币
     */
    private String currency;
    /**
     * 成本价Json
     */
    @TableField("cost_json")
    private String costJson;
    /**
     * 销售价Json
     */
    @TableField("sale_json")
    private String saleJson;


    /**
     * 有效期,分钟
     */
    @TableField("validate_time")
    private Long validateTime = 180L;
    /**
     * 单位
     */
    private String unit;

    /**
     * 2-手动维护，需要定时清除
     */
    private Integer type;


    /**
     * 库存基本信息
     */
    @TableField(exist = false)
    private ComponentStock stockInfo;

    @TableField(exist = false)
    private String costPriceText;
    @TableField(exist = false)
    private String priceText;
    @TableField(exist = false)
    private String numText;
    @TableField(exist = false)
    private List<ComponentPrice> children;

    @TableField(exist = false)
    private String matchDiscount;

    @TableField(exist = false)
    private String matchPrice;

    @TableField(exist = false)
    private String matchCostPrice;

    private LocalDateTime expireDate;

    //数量
    @TableField(exist = false)
    private Integer qty;

    @TableField(exist = false)
    private Integer matchingQuantity;

    private String uniqueId;

    public ComponentPrice(String uniqueId, String json, Long stock, String packaging, String encapsulation, String currency, String model, String costJson, String saleJson, BigDecimal expireDate) {
        this.uniqueId = uniqueId;
        this.json = json;
        if (stock == null) {
            stock = 0L;
        }
        this.stock = stock;
        this.packaging = packaging;
        this.encapsulation = encapsulation;
        this.currency = currency;
        this.model = model;
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
        this.costJson = costJson;
        this.saleJson = saleJson;
        this.expireDate = createTime.plusHours(expireDate.longValue());

    }

    public ComponentPrice(Long stockId, String json, Long stock, String packaging, String encapsulation, String currency, String model, String costJson, String saleJson) {
        this.stockId = stockId;
        this.json = json;
        if (stock == null) {
            stock = 0L;
        }
        this.stock = stock;
        this.packaging = packaging;
        this.encapsulation = encapsulation;
        this.currency = currency;
        this.model = model;
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
        this.costJson = costJson;
        this.saleJson = saleJson;

    }
}
